﻿using System;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;



[System.Web.Services.WebService(
   Namespace = "http://microsoft.com/webservices/",
   Description = "Crud operations on the Entity Centre in the Courier database")]
public class Service : System.Web.Services.WebService
{
 
    String MyConString = "Server=horseshow.com; Database=hsw;Uid=api; password=babd836a9;";
    public Service()
    {
        
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    
  
    public struct Photo
    {
       
        public String Title;
        public String UrlCore;
        public String UrlVideo;

    }

    //Struct for the videos and images to display in the home page

    public struct Video
    {
        public int videoId;
        public String videoPath;
        public String videoPicture;

    }

    // Struct for the has value to allow upload videos
    public struct Token
    {
        public String tokenId;
        
       
    }
    [WebMethod]
    public string test()
    {
        string test = "this is testing";
        return test;
    }
    [WebMethod]
    public string test2(string te)
    {
        string test2 = te;
        return test2;
    }

    [WebMethod]
    public Video[] get100Video()
    {
        MySqlConnection conn = new MySqlConnection(MyConString);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "SELECT * FROM videos WHERE status='C';";
        Video[] Videos = null;
        int i = 0;
        int a = 0;
        Videos = new Video[100];
        try
        {
            conn.Open();
            MySqlDataReader reader = command.ExecuteReader();


            while (reader.Read())
            {

                if (!reader["vid_photo"].ToString().Equals(""))
                {

                    //items.SetValue(reader["vid_photo"].ToString(), i);
                    Videos[i].videoId = Convert.ToInt32(reader["videoId"].ToString());
                    Videos[i].videoPath = reader["vid_filename"].ToString();
                    Videos[i].videoPicture = reader["vid_photo"].ToString();

                    i++;
                    a++;
                }
            }
        }
        catch (Exception ex)
        {

        }

        conn.Close();
        return Videos;

    }

   
    [WebMethod]
    //[XmlInclude(typeof(Centre))]
    //public Token[] getSaltByUserName(string username, string password)
    public string getSaltByUserName(string username, string password)
    {

        string token = "";
        bool status = false;
        string hash = "";
        string temp = "";
        MySqlConnection conn = new MySqlConnection(MyConString);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "Select * from membership where email_address=(?username);";
        command.Parameters.Add("?username", username);
        try
        {
            conn.Open();
            MySqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                temp = reader["password_salt"].ToString();
                status = true;
                
            }

        }
        catch (Exception ex)
        {
        }
        conn.Close();

        if (status == true)
        {
            
            byte[] buffer = Encoding.UTF8.GetBytes(password + temp);
            SHA1CryptoServiceProvider cryptoTransformSHA1 =
            new SHA1CryptoServiceProvider();
            hash = BitConverter.ToString(
            cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
            token = hash;
        }


        command.CommandText = "Select * from membership where passsword=(?password);";
        command.Parameters.Add("?password", hash);

        status = false;
        try
        {
            conn.Open();

            MySqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {



                token = reader["hash"].ToString();
                status = true;
                // Tokens[0].tokenId  = reader["hash"].ToString();

            }

        }
        catch (Exception ex)
        {

        }
        conn.Close();
        if (status == true)
        {
            token = "";
        }
        return token;
      /*  Token[] Tokens = null;
        Tokens = new Token[1];

        
        
       
        bool status=false;
        string hash = "";
        string temp="";
        MySqlConnection conn = new MySqlConnection(MyConString);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "Select * from membership where email_address=(?username);";
        command.Parameters.Add("?username", username);
        try
        {
            conn.Open();
            MySqlDataReader reader = command.ExecuteReader();
           
            while (reader.Read())
            {
                temp = reader["password_salt"].ToString();
                status = true;
                
            }

        }
        catch (Exception ex)
        {
        }
        conn.Close();
           

        if (status == true)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(password + temp);
            SHA1CryptoServiceProvider cryptoTransformSHA1 =
            new SHA1CryptoServiceProvider();
            hash = BitConverter.ToString(
            cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
            
        }

        
        command.CommandText = "Select * from membership where passsword=(?password);";
        command.Parameters.Add("?password", hash);

        
        try
        {
           conn.Open();
          
            MySqlDataReader reader = command.ExecuteReader();
            
            while (reader.Read())
            {

               

                 hash = reader["hash"].ToString();
               // Tokens[0].tokenId  = reader["hash"].ToString();
                
            }

       }
        catch (Exception ex)
       {

        }
        conn.Close();
        Tokens[0].tokenId = hash;
        return Tokens;*/
       

    }

     [WebMethod]
     public Photo[] GetAllPhoto()
     {
         

         MySqlConnection conn = new MySqlConnection(MyConString);
         MySqlCommand command = conn.CreateCommand();

         //command.CommandText = "SELECT * FROM videos  where  videoId BETWEEN 1 AND 100 and status='C'; ";
         command.CommandText = "SELECT * FROM videos  where status='C'; ";
         Photo[] Photos = null;

         int i = 0;
         int a = 0;
         Photos = new Photo[100];
         try
         {
             conn.Open();
             MySqlDataReader reader = command.ExecuteReader();


             while (reader.Read())
             {

                 if (!reader["vid_photo"].ToString().Equals(""))
                 {

                     //items.SetValue(reader["vid_photo"].ToString(), i);
                     Photos[i].Title = reader["status"].ToString();
                     Photos[i].UrlCore = reader["vid_photo"].ToString();
                     Photos[i].UrlVideo = reader["vid_filename"].ToString();

                     i++;
                     a++;
                 }
             }
         }
         catch (Exception ex)
         {
             
         }

         conn.Close();
         return Photos;
  
     }

}

   
